package com.cyt.web.controller.wx;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.ProProperties;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysUser;
import com.cyt.web.controller.base.BaseController;
import com.cyt.wx.model.po.WxApply;
import com.cyt.wx.model.vo.WxAccountVo;
import com.cyt.wx.model.vo.WxApplyVo;
import com.cyt.wx.service.WxAccountService;
import com.cyt.wx.service.WxApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 应用管理-应用信息控制层
 *
 * @author rong yuan
 * @date 2019-8-16 8:45:15
 */
@RestController
public class WxApplyController extends BaseController {

    @Resource
    private WxApplyService wxApplyService;
    @Resource
    private WxAccountService wxAccountService;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @RequestMapping("/wx/wxapply/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 分页页面
     *
     * @return Model
     * @author shengQiang Yu
     * @date 2019-8-16 8:45:15
     */
    @RequestMapping("/wx/wxapply/wxApplyList")
    public ModelAndView wxApplyList() {
        ModelAndView modelAndView=new ModelAndView("/wx/wxapply/wxApplyList");
        modelAndView.addObject("baseUrl", ProProperties.getInstance().getBaseUrl());
        return modelAndView;
    }
    /**
     * 分页查询
     *
     * @param request   请求对象
     * @param wxApplyVo 接受参数对象
     * @param page      当前页面
     * @param limit     页面数量
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @BaseLog(module = "应用管理", classz = "应用信息", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapply/page", method = RequestMethod.POST)
    public ReturnResult page(HttpServletRequest request, WxApplyVo wxApplyVo, int page, int limit) {
        SysUser sysUser = acceptCurrentUser(request);
        if (sysUser != null&& DictCode.SYS_USER_TYPE_SYYH.equalsIgnoreCase(sysUser.getUserType())) {
            wxApplyVo.setUserId(sysUser.getId());
        }
        return wxApplyService.page(wxApplyVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param request   请求对象
     * @param wxApplyVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @BaseLog(module = "应用管理", classz = "应用信息", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapply/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(HttpServletRequest request, WxApplyVo wxApplyVo) {
        ModelAndView view = new ModelAndView("/wx/wxapply/wxApplyEdit");
        if (StringUtils.isNotBlank(wxApplyVo.getId())) {
            view.addObject(wxApplyService.selectOne(wxApplyVo));
        }
        WxApply wxApply = acceptCurrentApplyId(request);
        if (wxApply != null) {
            view.addObject("applyId", wxApply.getId());
        } else {
            view.addObject("applyId", "");
            WxAccountVo wxAccountVo = new WxAccountVo();
            wxAccountVo.setAccountType(String.valueOf(1));
            view.addObject(wxAccountService.listExample(wxAccountVo));
        }
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request   请求对象
     * @param wxApplyVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @BaseLog(module = "应用管理", classz = "应用信息", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapply/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, WxApplyVo wxApplyVo) {
        SysUser sysUser = acceptCurrentUser(request);
        if (sysUser == null) {
            return new ReturnResult(ErrorCode.CODE_1, "请先登录");
        }
        return wxApplyService.addOrUpdate(wxApplyVo, sysUser);
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request   请求对象
     * @param wxApplyVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @BaseLog(module = "应用管理", classz = "应用信息", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapply/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, WxApplyVo wxApplyVo) {
        return wxApplyService.remove(wxApplyVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request   请求对象
     * @param wxApplyVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-16 8:45:15
     */
    @BaseLog(module = "应用管理", classz = "应用信息", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapply/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, WxApplyVo wxApplyVo) {
        return wxApplyService.delete(wxApplyVo, acceptCurrentUser(request));
    }
}